package cliente;

import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entityBean.Cliente;

import sessionBean.InfoClienteCtrlRemote;

/**
 * Servlet implementation class ModDatiClServlet
 */
public class ModDatiClServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModDatiClServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			processaRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			processaRichiesta(request, response);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private InfoClienteCtrlRemote getInfoClienteCtrl() throws NamingException {

		Properties p = new Properties();
		p.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
		p.setProperty("java.naming.provider.url", "localhost:1099");
		p.setProperty("java.naming.url.pkgs", "org.jboss.naming");

		Object obj = new InitialContext(p).lookup("InfoClienteCtrl/remote");

		return (InfoClienteCtrlRemote)PortableRemoteObject.narrow(obj, InfoClienteCtrlRemote.class);

	}
	
	private void processaRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {
		
		String from = request.getParameter("from");
		String cliente = request.getParameter("cliente");
		
		InfoClienteCtrlRemote gestore = getInfoClienteCtrl();
		
		if(from.equals("cliente")) {
			
			//reindirizzamento alla pagina di modifica passando il codice del cliente
			response.sendRedirect("ModInfoCl.jsp");
			
		}
		
		else {
			
			String telefono = request.getParameter("telefono");
			String email = request.getParameter("email");
			String pagamento = request.getParameter("pagamento");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			
			Cliente c = new Cliente();
			
			c.setCodFis(cliente);
			c.setTelefono(telefono);
			c.setEmail(email);
			c.setPagamento(pagamento);
			c.setUsername(username);
			c.setPassword(password);
			
			Cliente cl = gestore.modifica(c);
			
			//Passare il cliente cl alla pagina jsp
			response.sendRedirect("Cliente.jsp");
			
		}
		
	}

}
